/**
 * 
 */
package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.modeler.uml.ResourceFactory;

/**
 * @author YanYan
 *
 */
public class ActivityPartitionFigure extends CommonFigure {

	public static final org.eclipse.swt.graphics.Color INITIALNODE_COLOR = new Color(null,175,178,239);
	private static final int NAME_LABEL_Y_OFFSET = 10;
	private static final int NAME_LABEL_X_OFFSET = 20;
	public IFigure root = null;
	private Label nameLabel;
	
	public ActivityPartitionFigure(){
		super();
		
		XYLayout xyL = new XYLayout();
		setLayoutManager(xyL);
//		setLayoutManager(new FlowLayout());
		setOpaque(false);
		setBorder(new LineBorder(1));
		
		nameLabel = new Label("");
		nameLabel.setFont(ResourceFactory.getHeadFont());
		this.add(nameLabel);
	}
	public void setName(String name) {
		this.nameLabel.setText(name);
		this.refreshNameLabel();
	}
	private void refreshNameLabel(){
		Dimension size = nameLabel.getPreferredSize().getCopy();
		Rectangle bounds = this.getBounds();
		Rectangle labelBounds = new Rectangle();
		labelBounds.x = bounds.x + bounds.width - (size.width + this.NAME_LABEL_X_OFFSET);
		labelBounds.y = bounds.y + this.NAME_LABEL_Y_OFFSET;
		labelBounds.setSize(size);
		nameLabel.setBounds(labelBounds);
		
	}
	
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		refreshNameLabel();
	}
	protected void paintShadow(Graphics g) {
	}
	
	@Override
	public void paint(Graphics arg0) {
		super.paint(arg0);
		fillShape(arg0);
	}
	
	protected void fillShape(Graphics graphics) {
		graphics.pushState();
		graphics.setAntialias(SWT.ON);
		Rectangle recArea = getBounds();

		Point left = new Point(recArea.x, (int)(recArea.y+0.5*recArea.height));
		Point right = new Point((int)(recArea.x+recArea.width), (int)(recArea.y+0.5*recArea.height));
		Point up = new Point((int)(recArea.x+0.5*recArea.width), recArea.y);
		Point down = new Point((int)(recArea.x+0.5*recArea.width), (int)(recArea.y+recArea.height));
		
		/*
		if(recArea.width > recArea.height){
			graphics.drawLine(left, right);
		}else{
			graphics.drawLine(up, down);
		}
		*/
		graphics.popState();
	}
}
